package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.CaliItem;
import com.sctvcloud.dongqu.ui.fragment.CalligrapherFragment;
import com.sctvcloud.dongqu.ui.fragment.CaligrapherSubFragment;
import com.sctvcloud.dongqu.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.dongqu.ui.fragment.HuachengFragment;
import com.sctvcloud.dongqu.ui.fragment.ShareFragment;

import java.util.ArrayList;
import java.util.List;

public class CaligrapherPagerAdapter extends FragmentStatePagerAdapter {
    private Context context;
    public static final int TYPE_NEWS = 1;//最新新闻
    public static final int TYPE_GROUP = 2;//圏

    private List<CaliItem> dataList = new ArrayList<>();
    private boolean is_calli;

    public void setIs_calli(boolean is_calli) {
        this.is_calli = is_calli;
    }

    public CaligrapherPagerAdapter(Context context, FragmentManager fm, List<CaliItem> list) {
        super(fm);
        if(null!=list&&list.size()>0) {
            dataList.clear();
            this.dataList.addAll(list);
        }
        this.context = context;
    }


    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        switch (dataList.get(position).getType()) {
            case TYPE_GROUP:
                fragment = new ShareFragment();
                Bundle bundle1 = new Bundle();
                if(is_calli){
                    bundle1.putBoolean("is_calli",is_calli);
                    bundle1.putString("channelId", String.valueOf(dataList.get(position).getChannelId()));
                }
                fragment.setArguments(bundle1);
                break;
            case TYPE_NEWS:
            default:
                fragment = new CaligrapherSubFragment();
                Bundle bundle = new Bundle();
                bundle.putString("channelurl", dataList.get(position).getTabListurl());
                fragment.setArguments(bundle);
                break;
        }
        fragment.setIndexInViewPager(position);
        return fragment;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

//    @Override
//    public CharSequence getPageTitle(int position) {
//        return dataList.get(position).getTitle();
//    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
        ImageView iv = (ImageView) view.findViewById(R.id.tab_img);
        TextView tv = (TextView) view.findViewById(R.id.tab_text);
        tv.setText(dataList.get(position).getTitle());
//        iv.setImageDrawable(context.getResources().getDrawable(dataList.get(position).getNormalImg()));
        return view;
    }

    public void setData(List<CaliItem> data) {
        if((dataList==null||dataList.size()==0)&&(null!=data&&data.size()>0)) {
            dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return dataList.get(position).getTitle();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }
}
